package com.co.vote.web.controller.vote;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.co.server.constant.Constant;
import com.co.server.entity.vote.TClass;
import com.co.server.entity.vote.aide.TClassQuery;
import com.co.server.service.vote.TClassService;
import com.co.vote.constant.HttpStatusCode;
import com.co.common.utils.PageReq;

@Controller
@RequestMapping("/class")
public class ClassController {

	@Resource
	private TClassService tClassService;

//左侧列表，点击左侧刷新右侧
	//右侧详情列表
	//分类之前有最新，最热列表，从首页点进来
	//点击首页分类进来，点击别的页面label进来相当于搜索
	
	@RequestMapping(value = "/list", method = { RequestMethod.GET, RequestMethod.POST })
	public String list( Model model,HttpSession session,PageReq pageReq,TClassQuery query ) {
		query.setStatus(Constant.STATUS_ACTIVE);
		Page<TClass> page = tClassService.queryPageList(query, pageReq);
		model.addAttribute("page", page);
		return "/class/list";
	}
	
	@ResponseBody
	@RequestMapping(value = "/ajaxList", method = { RequestMethod.GET, RequestMethod.POST })
	public Map<String, Object> ajaxList( Model model,HttpSession session,PageReq pageReq,TClassQuery query ) {
		query.setStatus(Constant.STATUS_ACTIVE);
		pageReq.setPageSize(20);
		Page<TClass> page = tClassService.queryPageList(query, pageReq);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("page", page);
		result.put("code", HttpStatusCode.CODE_SUCCESS);
		return result;
	}

	@RequestMapping(value = "/add", method = { RequestMethod.GET,RequestMethod.POST })
	public String addInit( Model model,HttpSession session) {
		return "class/add";
	}

	@RequestMapping(value = "/edit/{id}/{pageNumber}", method = { RequestMethod.GET,RequestMethod.POST })
	public String edit( Model model,HttpSession session,@PathVariable Integer id, @PathVariable Integer pageNumber) {
		TClass tClass = tClassService.queryById(id);
		model.addAttribute("tClass", tClass);
		model.addAttribute("pageNumber", pageNumber);
		return "class/edit";
	}

	@RequestMapping(value = "/save", method = { RequestMethod.GET,RequestMethod.POST })
	public String save(Model model,HttpSession session,TClass tClass,PageReq pageReq,RedirectAttributes redirectAttributes) {
		if(tClass.getId()==null){
			tClassService.add(tClass);
		}else{
			tClassService.updateByIdSelective(tClass);
		}
		redirectAttributes.addAttribute("pageNumber", pageReq.getPageNumber());
		return "redirect:/class/list";
	}

	@ResponseBody
	@RequestMapping(value = "/delete/{id}", method = { RequestMethod.GET,RequestMethod.POST })
	public Map<String, Object> delete(@PathVariable Integer id) {
		tClassService.deleteById(id);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", HttpStatusCode.CODE_SUCCESS);
		return result;
	}
}
